
package com.example.cia.database.mysql.dao.impl;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.example.cia.database.mysql.dao.IDeviceDAO;
import com.mysql.jdbc.Connection;


public class addDeviceImpl extends mysqlDaoImpl implements IDeviceDAO{

	@Override
	public String getTableName(){
		return "device";
	}
	
	@Override
	public int add(String id,String description,int state) throws ClassNotFoundException, SQLException, IOException{
		String sql = "INSERT INTO "+getQulifiedTableName()+" (device_id,device_description,device_state) VALUES(?,?,?)";
		databaseManager db = databaseManager.createInstance();
		Connection con = db.getCon();
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setString(1, id);
		ps.setString(2, description);
		ps.setInt(3, state);
		return ps.executeUpdate();
	}

	@Override
	public String getQueryFieldString(int which){
		return getTableName()+"_state";
	}
	
	@Override
	public int modify(String id, int state) throws SQLException {
		String sql = "UPDATE "+getQulifiedTableName()+" SET "
				+getQueryFieldString(0)+"="+state+" WHERE "+getPrimaryKey()+" = ?";
		databaseManager db = databaseManager.createInstance();
		Connection con = db.getCon();
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setString(1,id);
		return ps.executeUpdate();
	}
}
